<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const getJSON = function (url) {
            return new Promise((resolve, reject) => {
                // 1.创建XMLHttpRequest对象
                var xhr = new XMLHttpRequest();
                // 2.使用open方法设置请求方式和请求地址
                xhr.open('GET', url, true);
                xhr.setRequestHeader('Accept', 'application/json');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        resolve(xhr.responseText);
                    } else {
                        reject(new Error(xhr.responseText));
                    }
                }
                // 3.使用send方法发送请求
                xhr.send();
            })
        }
    </script>
</body>

</html>